

let count = 5//每页条数
let page = 1//当前页码
let maxPage//最大页码
let pageArr = [10, 15, 20, 25, 30, 35];//下拉显示页码
getData();
function getData() {
  $.ajax({
    url: '/jianli/get',
    method: 'get',
    data: {
      count: count,
      page: page
    },
    success: (res) => {
      console.log(res);
      if (res.error) {
        console.log(res.error)
      } else {
        $('tbody').html('');
        $(res.data).each((i, v) => {
          //数据渲染
          $('tbody').append(`
          <tr data-id="${v.jianli_id}">
              <td>${v.jianli_id}</td>
              <td>${v.jianli_z_id}</td>
              <td>${v.jianli_xm}</td>
              <td>${v.jianli_xb}</td>
              <td>${v.jianli_nl}</td>
              <td>${v.jianli_xl}</td>
              <td>${v.jianli_xy}</td>
              <td>${v.jianli_jy}</td>
              <td>${v.jianli_tdsj}</td>
              <td>
              <button class="btn_com btn_frameless"><span class="iconfont icon-pen"></span>查看详情</button>
              <button class="btn_del btn_frameless"><span class="iconfont icon-lajitong"></span>删除</button>
            </td>
            </tr>
          `)
        })
        $('.pagination').html(`     
        <li id="page_left">
            <a href="#" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
        <li id="page_right">
            <a href="#" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
        `);
        maxPage = Math.ceil((res.total / count));
        paging('#page_right', maxPage, page);
      }
    }
  })
}
$('.pagination').on('click', '.new_page', function () {
  page = parseInt($(this).children().text());
  $('.new_page').eq(page - 1).toggleClass("active");
  getData();
});
// 上一页
$('.pagination').on('click', '#page_left', function () {
  page > 1 ? page-- : page = 1
  $('.new_page').eq(page - 1).toggleClass("active");
  getData();
});
// 下一页
$('.pagination').on('click', '#page_right', function () {
  page < maxPage ? page++ : page = maxPage
  $('.new_page').eq(page - 1).toggleClass("active");
  getData();
});

//渲染每页多少条下拉框
window.onload = () => {
  pageArr.forEach(item => {
    $('#tiaoshu').append(
      `
      <li data-num="${item}">
                   <a href="#">每页显示 ${item} 条</a>
                </li> 
      `
    )
  })
}

$('.dropdown-menu').on('click', 'li', function () {
  count = parseInt($(this).attr('data-num'));
  getData();
});
// 输入框查询
$('.aaa>a').click(function(){
  // cd = $('#bumen option:selected').val()
  // console.log(cd)
  $.ajax({
    url:'/jianli/input',
    method:'get',
    data:{
      jianli_xm:$('.drop_down_xingm').val(),
      count: count,
      page: page
    },
    success: (res) => {
      console.log(res)
      if (res.error) {
        console.log(res.error)
      }else{
        $('tbody').html('');
        $(res.data).each((i, v) => {
          //数据渲染
          $('tbody').append(`
          <tr data-id="${v.jianli_id}">
              <td>${v.jianli_id}</td>
              <td>${v.jianli_z_id}</td>
              <td>${v.jianli_xm}</td>
              <td>${v.jianli_xb}</td>
              <td>${v.jianli_nl}</td>
              <td>${v.jianli_xl}</td>
              <td>${v.jianli_xy}</td>
              <td>${v.jianli_jy}</td>
              <td>${v.jianli_tdsj}</td>
              <td>
              <button class="btn_com btn_frameless"><span class="iconfont icon-pen"></span>查看详情</button>
              <button class="btn_del btn_frameless"><span class="iconfont icon-lajitong"></span>删除</button>
            </td>
            </tr>
          `)
        })
        $('.pagination').html(`     
        <li id="page_left">
            <a href="#" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
        <li id="page_right">
            <a href="#" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
        `);
        maxPage = Math.ceil((res.total / count));
        // paging('#page_right', maxPage, page);
        for (let i = 1; i <= maxPage; i++) {
          $('#page_right').before(`
                      <li class="page_num"><a href="#">${i}</a></li>
                  `);
          if (page == i) {
            $('.page_num').eq(i - 1).toggleClass("active");
          }
        };
        $('.pagination').css({
          display: 'none'
        });
      }
    }
  })
})
//部门查询
$('#bumen').click(function(){
  cd = $('#bumen option:selected').val()
  console.log(cd)
  $.ajax({
    url:'/jianli/bumen',
    method:'get',
    data:{
      jianli_xy:$('#bumen option:selected').val(),
      count: count,
      page: page
    },
    success: (res) => {
      console.log(res)
      if (res.error) {
        console.log(res.error)
      }else{
        $('tbody').html('');
        $(res.data).each((i, v) => {
          //数据渲染
          $('tbody').append(`
          <tr data-id="${v.jianli_id}">
              <td>${v.jianli_id}</td>
              <td>${v.jianli_z_id}</td>
              <td>${v.jianli_xm}</td>
              <td>${v.jianli_xb}</td>
              <td>${v.jianli_nl}</td>
              <td>${v.jianli_xl}</td>
              <td>${v.jianli_xy}</td>
              <td>${v.jianli_jy}</td>
              <td>${v.jianli_tdsj}</td>
              <td>
              <button class="btn_com btn_frameless"><span class="iconfont icon-pen"></span>查看详情</button>
              <button class="btn_del btn_frameless"><span class="iconfont icon-lajitong"></span>删除</button>
            </td>
            </tr>
          `)
        })
        $('.pagination').html(`     
        <li id="page_left">
            <a href="#" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
        <li id="page_right">
            <a href="#" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
        `);
        maxPage = Math.ceil((res.total / count));
        // paging('#page_right', maxPage, page);
        for (let i = 1; i <= maxPage; i++) {
          $('#page_right').before(`
                      <li class="page_num"><a href="#">${i}</a></li>
                  `);
          if (page == i) {
            $('.page_num').eq(i - 1).toggleClass("active");
          }
        };
        $('.pagination').css({
          display: 'none'
        });
      }
    }
  })
})
//学历查询
$('#xueli').click(function(){
  cd = $('#xueli option:selected').val()
  console.log(cd)
  $.ajax({
    url:'/jianli/xueli',
    method:'get',
    data:{
      jianli_xl:$('#xueli option:selected').val(),
      count: count,
      page: page
    },
    success: (res) => {
      console.log(res)
      if (res.error) {
        console.log(res.error)
      }else{
        $('tbody').html('');
        $(res.data).each((i, v) => {
          //数据渲染
          $('tbody').append(`
          <tr data-id="${v.jianli_id}">
              <td>${v.jianli_id}</td>
              <td>${v.jianli_z_id}</td>
              <td>${v.jianli_xm}</td>
              <td>${v.jianli_xb}</td>
              <td>${v.jianli_nl}</td>
              <td>${v.jianli_xl}</td>
              <td>${v.jianli_xy}</td>
              <td>${v.jianli_jy}</td>
              <td>${v.jianli_tdsj}</td>
              <td>
              <button class="btn_kan btn_frameless"><span class="iconfont icon-pen"></span>查看详情</button>
              <button class="btn_del btn_frameless"><span class="iconfont icon-lajitong"></span>删除</button>
            </td>
            </tr>
          `)
        })
        $('.pagination').html(`     
        <li id="page_left">
            <a href="#" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
        <li id="page_right">
            <a href="#" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
        `);
        maxPage = Math.ceil((res.total / count));
        // paging('#page_right', maxPage, page);
        for (let i = 1; i <= maxPage; i++) {
          $('#page_right').before(`
                      <li class="page_num"><a href="#">${i}</a></li>
                  `);
          if (page == i) {
            $('.page_num').eq(i - 1).toggleClass("active");
          }
        };
        $('.pagination').css({
          display: 'none'
        });
      }
    }
  })
})
//删除
$('tbody').on('click', '.btn_del', function () {
  if(confirm('确定要删除吗？')){
    $.ajax({
      url: '/jianli/del',
      method: 'post',
      data: {
        jianli_id: $(this).parents('tr').attr('data-id')
      },
      success: (res) => {
        console.log(res);
        if (res.reeor) {
          console.log(res.error)
        } else {
          alert('删除成功')
          getData();
        }
      }
    })
  }
  
})


